package ecom.appcode.dao.daointerface;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ecom.appcode.common.daobean.CategoryDaoBean;
import ecom.appcode.common.daobean.SliderDaoBean;
import ecom.appcode.common.utility.DBUltils;

public class SliderDaoImpl implements SliderDao{
    
    /** Add New Slider */
    public boolean addNewSlider(String image, String description) {
        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = DBUltils.makeConnection();
            String sql = "INSERT INTO tblSlider VALUES(?,?)";
            stm = con.prepareStatement(sql);

            stm.setString(1, image);
            stm.setString(2, description);

            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    
    /** Load All Slider */
    public List<SliderDaoBean> loadAllSlider() {
        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        try {
            con = DBUltils.makeConnection();
            String sql = "SELECT * FROM tblSlider";
            stm = con.prepareStatement(sql);
            rs = stm.executeQuery();
            List<SliderDaoBean> list = new ArrayList<SliderDaoBean>();
            SliderDaoBean slider = new SliderDaoBean();
            while (rs.next()) {
                /** Slider ID */
                int id = rs.getInt("id");
                /** Slider Url */
                String image = rs.getString("image");
                /** Description */
                String description = rs.getString("description");

                 slider = new SliderDaoBean(id, image, description);

                list.add(slider);
            }
            return list;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }
        return null;
    }
    
    /** Delete Slider */
    public  boolean deleteSlider(int sliderID) {
        Connection con = null;
        PreparedStatement stm = null;

        try {
            con = DBUltils.makeConnection();
            String sql = "DELETE FROM tblSlider WHERE id = ?";
            stm = con.prepareStatement(sql);
            stm.setInt(1, sliderID);

            int row = stm.executeUpdate();
            if (row > 0) {
                return true;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stm != null) {
                    stm.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (con != null) {
                    con.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}
